package com.t11;

import java.util.Iterator;

/**
 * Created by xuyiwei on 2018/7/22.
 */
public class IterableClass implements Iterable<String> {
    private String [] words = ("And that is how we know the Earth to be banana-shaped").split(" ");
    @Override
    public Iterator<String> iterator() {
        return new Iterator<String>() {
            private int index = 0;
            @Override
            public boolean hasNext() {
                return index<words.length;
            }

            @Override
            public String next() {
                return words[index++];
            }
        };
    }

    public static void main(String[] args) {
        //迭代器直接可以拿for循环遍历
        for(String s:new IterableClass()){
            System.out.println(s+" ");
        }
    }
}
